//
//  LoginViewController.m
//  XMPPMessage
//
//  Created by 刘永杰 on 15/11/20.
//  Copyright © 2015年 刘永杰. All rights reserved.
//

#import "LoginViewController.h"
#import "XMPPManager.h"
#import "AppDelegate.h"
#import "RosterViewController.h"

@interface LoginViewController ()<XMPPStreamDelegate>
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[XMPPManager defaultManager].XMPPStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
    
    
    // Do any additional setup after loading the view.
}

- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error
{
    NSLog(@"登录失败");
}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
    //发送一个上线的消息
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];
    //上线的消息要通过XMPPStream发送
    [[XMPPManager defaultManager].XMPPStream sendElement:presence];
    
    NSLog(@"登录成功");
    
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    
    RosterViewController *roster = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"RosterVC"];
    app.window.rootViewController = roster;
    
}

- (IBAction)loginAction:(id)sender {
    
    //调用XMPPManager里面的登录注册方法
    [[XMPPManager defaultManager] loginWithUserName:self.userName.text andPassword:self.password.text];
    
    
}
- (IBAction)registerAction:(id)sender {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
